<template>
  <el-card class="sku-list">
    <div class="test">
      <span>aaaaaa</span>
    </div>
    <el-table
      border
      stripe
      :data="tableData.list"
      v-loading="tableData.loading"
      row-key="id"
    >
      <el-table-column type="index" label="序号" width="80" align="center" />

      <el-table-column prop="skuName" label="名称" />

      <el-table-column prop="skuDesc" label="描述" />

      <el-table-column label="默认图片" width="150" align="center">
        <template v-slot="{ row }">
          <el-image
            :src="row.skuDefaultImg"
            alt="商品图片"
            style="width: 80px; height: 80px"
          />
        </template>
      </el-table-column>

      <el-table-column prop="weight" label="重量(KG)" />

      <el-table-column prop="price" label="价格(元)" />

      <el-table-column label="操作" width="250" align="center">
        <template v-slot="{ row }">
          <el-button
            v-if="row.isSale == 0"
            title="上架"
            type="info"
            size="small"
            :icon="Top"
            @click="onSale(row.id)"
          />
          <el-button
            v-else
            title="下架"
            type="success"
            size="small"
            :icon="Bottom"
            @click="cancelSale(row.id)"
          />
          <el-button
            title="修改"
            type="primary"
            size="small"
            :icon="Edit"
          />
          <el-button
            title="查看详情"
            type="info"
            size="small"
            :icon="InfoFilled"
            @click="showSkuInfo(row.id)"
          />
          <el-button
            style="margin-left: 10px"
            type="danger"
            size="small"
            :icon="Delete"
            title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="tableData.page"
      v-model:page-size="tableData.pageSize"
      :total="tableData.total"
      :page-sizes="[10, 20, 30]"
      style="padding: 20px 0; text-align: center"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @current-change="getList"
      @size-change="getList"
    />

    <el-drawer
      v-model="skuInfoData.isShowInfo"
      :with-header="false"
      size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{skuInfoData.skuInfo?.skuName}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{skuInfoData.skuInfo?.skuDesc}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{skuInfoData.skuInfo?.price}}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="18">
          <div v-if="skuInfoData.skuInfo?.skuAttrValueList && skuInfoData.skuInfo?.skuAttrValueList.length>0">
            <el-tag type="success" style="margin-right: 5px" 
              v-for="value in skuInfoData.skuInfo?.skuAttrValueList" :key="value.id">
                {{value.attrName + ' / ' + value.valueName}}
            </el-tag>
          </div>
          <div v-else>无</div>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">销售属性</el-col>
        <el-col :span="18">
          <el-tag type="success" style="margin-right: 5px" 
            v-for="value in skuInfoData.skuInfo?.skuSaleAttrValueList" 
            :key="value.id"
            v-if="skuInfoData.skuInfo?.skuSaleAttrValueList && skuInfoData.skuInfo?.skuSaleAttrValueList.length>0">
            {{value.saleAttrName + ' / ' + value.saleAttrValueName}}
          </el-tag>
          <div v-else>无</div>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
           <el-carousel class="sku-carousel" trigger="click" height="400px" style="width: 400px">
              <el-carousel-item v-for="item in skuInfoData.skuInfo?.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="" style="width: 100%;height: 100%;">
              </el-carousel-item>
            </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>

  </el-card>
</template>

<script lang="ts">
export default {
  name: 'Sku',
};
</script>

<script lang="ts" setup>
import type { SkuListModel, SkuModel } from '@/api/product/model/skuModel';
import { cancelSaleApi, getSkuInfoApi, getSkuPageListApi, onSaleApi } from '@/api/product/sku';
import { Bottom, Delete, Edit, InfoFilled, Top } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { reactive, onMounted } from 'vue';


/* **显示sku详情 start** */
// sku详情相关数据
const skuInfoData = reactive<{
  isShowInfo: boolean; // 是否显示详情
  skuInfo?: SkuModel; // sku详情
}>({
  isShowInfo: false,
  skuInfo: undefined
})

// 显示sku详情
const showSkuInfo = async (id: number) => {
  // 发请求
  skuInfoData.skuInfo =  await getSkuInfoApi(id)
  // 显示
  skuInfoData.isShowInfo = true
  
}

/* **显示sku详情 end** */



/* **上下架 start** */
// 对指定SKU进行上架的请求
const onSale = (skuId: number) => {
  onSaleApi(skuId).then(() => {
    // ElMessage.success('上架成功!')
    ElMessage({
      type: 'success',
      message: '上架成功!'
    })
    getList()
  })
}

// 对指定SKU进行下架的请求
const cancelSale = (skuId: number) => {
  cancelSaleApi(skuId).then(() => {
    ElMessage({
      type: 'success',
      message: '下架成功!'
    })
    getList()
  })
}
/* **上下架 end** */


/* sku分页列表 start */
const tableData = reactive<{
  list: SkuListModel;
  total: number;
  page: number;
  pageSize: number;
  loading: boolean;
}>({
  list: [],
  total: 0,
  page: 1,
  pageSize: 10,
  loading: false
})

const getList = async () => {
  tableData.loading = true

  const result = await getSkuPageListApi(tableData.page, tableData.pageSize)

  tableData.loading = false
  tableData.list = result.records
  tableData.total = result.total
}

onMounted(() => {
  getList()
})
/* sku分页列表 end */
</script>

<style lang="scss" scoped>
  .sku-list {
    .test {
      color: red;
    }
    .el-row {  // Row组件的根标签
      margin: 10px 0;
      .el-col-5 { // Col组件的根标签
        text-align: right;
        font-size: 16px;
        margin-right: 20px;
      }
    }

    :deep(.el-carousel__indicator) {  // li / button   // Carousel的子标签
      button {
        background-color: pink;
      }
      &.is-active { // &称为父级引用
        button {
          background-color: hotpink;
        }
      }
    }
  }
</style>
